<script setup>
import api from '@/api/index.js'
import { message } from 'ant-design-vue'
import { computed, ref, h, provide } from 'vue'
import MyTable from '@/components/MyTable/MyTable.vue'
import { Modal } from 'ant-design-vue'

const props = defineProps({
  // 表格数据总条数
  total: { type: Number, default: 0 },
  // 表格当前页码
  pageNum: { type: Number, default: 1 }
})
// 分页参数
const pagination = computed(() => ({
  total: props.total,
  showSizeChanger: false,
  current: props.pageNum,
  onChange: (e) => {
    emits('pageChange', e)
  }
}))

const emits = defineEmits(['edit', 'pageChange', 'reload'])
// 表格列配置
const columns = ref([
  { ellipsis: true, title: '规则名称', dataIndex: 'name', key: 'name', },
  { ellipsis: true, title: '农机类别', dataIndex: 'typeName', key: 'typeName', },
  { ellipsis: true, title: '耕作起步面积及距离', dataIndex: 'workDesc', key: 'workDesc', },
  // { ellipsis: true, title: '耕作起步面积(亩)', dataIndex: 'startArea', key: 'startArea', },
  // { ellipsis: true, title: '耕作最远距离(km)', dataIndex: 'longDistance', key: 'longDistance', },
  // { ellipsis: true, title: '单价', dataIndex: 'unitPrice', key: 'unitPrice', },
  {
    ellipsis: true,
    title: '单价(元)',
    key: 'unitPrice',
    customRender: ({ record }) => { record.minUnitPrice, record.maxUnitPrice }
  },
  // { ellipsis: true, title: '补贴价格', dataIndex: 'subsidy', key: 'subsidy', },
  // { ellipsis: true, title: '补贴价格单位', dataIndex: 'subsidyUnit', key: 'subsidyUnit', },
  { ellipsis: true, title: '备注', dataIndex: 'notes', key: 'notes', },
  { ellipsis: true, title: '操作', key: 'set', width: '160px', align: 'center' },
])

// 编辑数据
const edit = (item) => {
  emits('edit', item)
}

//删除数据
const del = (item) => {
  Modal.warning({
    title: '删除结算设置',
    content: h(
      'span',
      {},
      '是否删除结算设置数据：',
      h('span', { style: { color: '#09c690' } }, item.name)
    ),
    okText: '删除',
    okCancel: true,
    okButtonProps: { type: 'primary', danger: true },
    cancelButtonProps: { type: 'primary' },
    cancelText: '取消',
    onOk: async () => {
      await api.delJssz({ id: item.id })
      message.success('删除成功')
      emits('reload')
      return Promise.resolve()
    },
    onCancel: () => {
      message.success('取消删除')
      return Promise.resolve()
    }
  })
}
provide('columns', columns.value)
</script>

<template>
  <MyTable :pagination="pagination" :scroll="{ y: 'calc(100vh - 383px)' }">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'section'">
        10公里10亩起，大于20公里50亩起，大于100公里200亩起
      </template>
      <template v-if="column.key === 'set'">
        <AButton type="link" @click="edit(record)">编辑</AButton>
        <AButton type="link" class="com-delbtn" @click="del(record)">删除</AButton>
      </template>
      <template v-if="column.key === 'unitPrice'">
        {{ record.minUnitPrice }} ~ {{ record.maxUnitPrice }}
      </template>
    </template>
  </MyTable>
</template>